package tv.youxing.android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.othershe.baseadapter.ViewHolder;
import com.othershe.baseadapter.base.CommonBaseAdapter;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import tv.youxing.android.R;
import tv.youxing.android.custom.GridViewForScrollView;
import tv.youxing.android.entity.DianZhanModel;
import tv.youxing.android.entity.DynamicDetailsPingLunModel;
import tv.youxing.android.entity.FriendModel;
import tv.youxing.android.entity.ImageArrayModel;
import tv.youxing.android.ui.DynamicDetailsActivity;
import tv.youxing.android.ui.LoginActivity;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

import static com.mob.MobSDK.getContext;

/**
 * Author: Othershe
 * Time: 2016/8/29 15:40
 */
public class FrientAdapter extends CommonBaseAdapter<FriendModel.ResultBean.DataBean> {


    Activity mActivity;
    Context mContext;

    public FrientAdapter(Activity activity,Context context, List<FriendModel.ResultBean.DataBean> datas, boolean isLoadMore) {
        super(context, datas, isLoadMore);


        this.mActivity = activity;
        this.mContext = context;
    }



    @Override
    protected void convert(ViewHolder holder, final FriendModel.ResultBean.DataBean data, final int position) {


        CircleImageView ivAvatar;
        TextView tvName;
        TextView tvContent;
        GridViewForScrollView gridView;
        TextView tvTime;
        final ImageView ivPraise;
        final TextView tvPraise;
        TextView tvComment;
        RelativeLayout llPinglun;


        ivAvatar = holder.getView(R.id.ivIcon);
        tvName = holder.getView(R.id.tvName);
        tvContent = holder.getView(R.id.tvContent);
        gridView = holder.getView(R.id.picGridView);
        tvTime = holder.getView(R.id.tvTime);
        ivPraise = holder.getView(R.id.ivPraise);
        tvPraise = holder.getView(R.id.tvPraise);
        tvComment = holder.getView(R.id.tvComment);
        llPinglun = holder.getView(R.id.pinglun);

        tvName.setText(data.getPublish_user().getUsername());
        //发布内容
        tvContent.setText(data.getContent());
        //发布的图片
        List<ImageArrayModel> imgs = new ArrayList<>();
        for (int i = 0; i < data.getImage_array().size(); i++) {
            ImageArrayModel model = new ImageArrayModel();

            model.setImage_url(data.getImage_array().get(i).getImage_url());
            model.setThumbnail(data.getImage_array().get(i).getThumbnail());
            imgs.add(model);
        }
        gridView.setAdapter(new ImageAdapter(mActivity,mContext, imgs));


        //时间
        tvTime.setText(data.getPublish_time());

        //点赞
        ivPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //post请求简洁版实现
                HttpParams params = new HttpParams();
                params.putHeaders("Authorization", "Bearer " + ShareUtils.getString(getContext(), AppConst.TOKEN, ""));
                params.put("resource_id", data.getTweet_id());
                params.put("resource_type", 2);


                RxVolley.post(StaticClass.DianZan, params, new HttpCallback() {
                    @Override
                    public void onSuccess(String t) {
                        DianZhanModel dianZhanModel = GsonUtils.GsonToBean(t, DianZhanModel.class);

                        if (dianZhanModel.getCode() == AppConst.TOKENERROR) {
                            mActivity.startActivity(new Intent(mActivity,LoginActivity.class));
                            return;
                        }

                        if (dianZhanModel.getCode() == 200) {
                            ivPraise.setSelected(true);
                            int count = Integer.parseInt(data.getLike_count()) + 1;
                            tvPraise.setText(count + "");
                        } else {
                            ivPraise.setSelected(false);
                        }
                    }
                });
            }
        });

        //点赞数
        tvPraise.setText(data.getLike_count());

        //说说评论数
        tvComment.setText(data.getComment_count());

        if (data.getIs_like() == 1) {
            tvPraise.setSelected(true);
        } else {
            tvPraise.setSelected(false);
        }

        //评论界面
        llPinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DynamicDetailsActivity.class);

                Bundle bundle = new Bundle();


                DynamicDetailsPingLunModel model = new DynamicDetailsPingLunModel();
                DynamicDetailsPingLunModel.PublishUserBean model_user = new DynamicDetailsPingLunModel.PublishUserBean();

                model.setContent(data.getContent());
                model.setPublish_time(data.getPublish_time());
                model.setIs_like(data.getIs_like());
                model.setTweet_id(data.getTweet_id());
                model.setLike_count(data.getLike_count());

                model_user.setAvatar(data.getPublish_user().getAvatar());
                model_user.setUsername(data.getPublish_user().getUsername());


                model.setPublish_user(model_user);


                bundle.putSerializable("data", model);


                intent.putExtras(bundle);
                mActivity.startActivity(intent);
            }
        });
    }


    @Override
    protected int getItemLayoutId() {
        return R.layout.layout_item_dynamic;
    }


}
